<?php

class Application_Model_UserService extends Base_Service
{
   /**
    * 
    * fetch user information
    * @param unknown_type $username user name
    * @param unknown_type $password user password
    */
	public function getUserInfo($username, $password) {
		$table = new Application_Model_DbTable_User();
		return $resultSet = $table-> fetchRow("username='".$username."' and password='".$password."'");
		if($resultSet == null ) {
			return null;
		} else {
			return $resultSet->toArray();
		}
	}
	
	/**
	 * 
	 * create a user
	 * @param unknown_type $username
	 * @param unknown_type $password
	 * @param unknown_type $nickname
	 */
    public function createUser($username, $password, $nickname) {
		$table = new Application_Model_DbTable_User();
		$db = $this->getDbAdapter();
		
		$row = array ( 
		'username' => $username,
		'password' => $password,
		'nickname' => $nickname);
		$this->logger->debug(__METHOD__. '   ' .$row);			
		$rows_affected = $db->insert('user', $row);
		return $this->getUserInfo($username, $password);
	}
	
	public function getAllUserInfo(){
		$dbAdapter = $this->getDbAdapter();
		return $resultSet = $dbAdapter-> fetchAll("select uid, nickname from user;");
		if($resultSet == null ) {
			return array();
		} else {
			return $resultSet->toArray();
		}
	}
	
	protected static $_instance = null;
	public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}

